#!/bin/bash

THIS_ETH0="ETH0=\`ifconfig | grep -A 1 'eth0' | grep inet | awk '{print \$2}'\`"
THIS_PS1='PS1="\n\[\033[01;33m\][\t] eth0 => \$ETH0 | path => \$PWD     \n\[\033[01;31m\]\u@\[\033[01;34m\]\h \[\033[01;34m\]\W \[\033[01;33m\]\\$ \[\033[00m\]"'

function addPsConfig() {

    cat <<EOF >>/etc/profile.d/PS1.sh

#命令行格式化定制
${THIS_ETH0}
${THIS_PS1}

EOF
}

function isResult() {
    case $1 in
    old)
        printMessage "已有参数替换成功"
        ;;
    new)
        printMessage "新增参数成功"
        ;;
    *)
        echo "失败"
        printMessage "参数变更失败"
        ;;
    esac
}

function printMessage() {
    cat <<iquanwai
========================================

${1}：

${THIS_ETH0}

${THIS_PS1}

========================================
iquanwai
}

if (grep "^PS1=" /etc/profile); then
    sed -i '/^PS1=/d' /etc/profile
    if (grep "^ETH0=" /etc/profile); then
        sed -i '/^ETH0=/d' /etc/profile
    fi
    addPsConfig
    isResult "old"
else
    echo ""
    addPsConfig
    isResult "new"
fi
